package lc.com.lcpicturebackend.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lc.com.lcpicturebackend.exception.BusinessException;
import lc.com.lcpicturebackend.exception.ErrorCode;
import lc.com.lcpicturebackend.exception.ThrowUtils;
import lc.com.lcpicturebackend.mapper.ConversationMsgMapper;
import lc.com.lcpicturebackend.model.dto.chatai.ConversationMsgQueryRequest;
import lc.com.lcpicturebackend.model.entity.Conversation;
import lc.com.lcpicturebackend.model.entity.ConversationMsg;
import lc.com.lcpicturebackend.model.entity.User;
import lc.com.lcpicturebackend.model.vo.chat.ConversationMsgVO;
import lc.com.lcpicturebackend.service.ConversationMsgService;
import lc.com.lcpicturebackend.service.ConversationService;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 罗超
 * @description 针对表【conversation_msg】的数据库操作Service实现
 * @createDate 2025-05-26 22:03:10
 */
@Service
public class ConversationMsgServiceImpl extends ServiceImpl<ConversationMsgMapper, ConversationMsg>
        implements ConversationMsgService {

    @Resource
    @Lazy
    private ConversationService conversationService;

    @Override
    @Async
    public void deleteConversationMsg(String conversationId) {
        ThrowUtils.throwIf(StrUtil.isBlank(conversationId), ErrorCode.PARAMS_ERROR);
        boolean result = this.lambdaUpdate()
                .eq(ConversationMsg::getConversationId, conversationId)
                .remove();
        ThrowUtils.throwIf(!result, ErrorCode.SYSTEM_ERROR, "删除会话消息失败");
    }

    @Override
    public ConversationMsgVO getConversationMsgListVO(ConversationMsgQueryRequest conversationMsgQueryRequest,
                                                          User loginUser) {
        if (StrUtil.isBlank(conversationMsgQueryRequest.getConversationId())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String conversationId = conversationMsgQueryRequest.getConversationId();
        Conversation conversation = conversationService.getById(conversationId);
        String sortField = conversationMsgQueryRequest.getSortField();
        String sortOrder = conversationMsgQueryRequest.getSortOrder();

        // 校验用户是否有权限
        ThrowUtils.throwIf(ObjectUtil.isEmpty(conversation), ErrorCode.PARAMS_ERROR, "会话不存在");
        if (!conversation.getUserId().equals(loginUser.getId())) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        // 获取消息列表
        QueryWrapper<ConversationMsg> conversationMsgQueryWrapper = new QueryWrapper<>();
        conversationMsgQueryWrapper.eq("conversationId", conversationId);
        conversationMsgQueryWrapper.orderBy(StrUtil.isNotBlank(sortField), !sortOrder.equals("descend"), sortField);

        // 查询数据库
        List<ConversationMsg> list = this.list(conversationMsgQueryWrapper);

        if (CollectionUtil.isEmpty(list)) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }

        // ConversationMsg 转 VO
        ConversationMsgVO conversationMsgVO = new ConversationMsgVO();
        conversationMsgVO.setConversation(conversation);
        conversationMsgVO.setConversationMsg(list);

        return conversationMsgVO;
    }
}




